<template>
  <div>
      <el-form :model="searchForm" inline label-width="80px">
        <el-form-item label="车牌号">
          <el-input v-model="searchForm.carNo" clearable placeholder="模糊匹配车牌"/>
        </el-form-item>
        <el-form-item label="品牌">
          <el-cascader :props="props" placeholder="选择品牌" v-model="searchForm.ps" filterable clearable></el-cascader>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="searchForm.type" clearable placeholder="选择类型">
            <el-option label="轿车" :value="1"/>
            <el-option label="SUV" :value="2"/>
            <el-option label="商务" :value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" clearable placeholder="选择状态">
            <el-option label="可用" :value="1"/>
            <el-option label="禁用" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataList">搜索</el-button>
          <el-button @click="clearForm">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="btn-group">
        <el-button type="primary" size="mini" @click="openDialog()">添加</el-button>
      </div>
      <el-table :data="dataList" border>
        <el-table-column prop="id" label="编号" align="center"/>
        <el-table-column prop="carNo" label="车牌" align="center"/>
        <el-table-column prop="color" label="颜色" align="center"/>
        <el-table-column prop="brandName" label="品牌名" align="center"/>
        <el-table-column prop="type" label="类型" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 1">轿车</el-tag>
            <el-tag v-else-if="scope.row.type === 2">SUV</el-tag>
            <el-tag v-else>商务</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status === 1" size="mini" type="success" @click="changeStatus(scope.row.id,2)">可用</el-button>
            <el-button v-else size="mini" type="danger" @click="changeStatus(scope.row.id,1)">禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column label="车辆照片" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="showImageList(scope.row)" type="text">查看照片</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160px">
          <template slot-scope="scope">
            <el-button size="mini" @click="openDialog(scope.row.id)">修改</el-button>
            <el-button size="mini" @click="deleteInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <el-pagination hide-on-single-page :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" @current-change="currentChange" @size-change="sizeChange" layout="total,sizes,prev,pager,next,jumper" :total="pageInfo.total"/>
    <car-update ref="carUpdate" @closeDialog="getDataList"/>
    <el-dialog :visible.sync="showImage" title="车辆照片" v-if="showImage">
      <el-carousel :interval="4000" >
        <el-carousel-item v-for="(item,index) in imageList" :key="index">
          <el-image :src="item.url"></el-image>
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
  </div>
</template>

<script>
import CarUpdate from '@/views/car/CarUpdate.vue'

const initData = {
  carNo: '',
  ps: [],
  status: '',
  type: '',
  brandId: ''
}
export default {
  name: 'CarList',
  components: { CarUpdate },
  data () {
    return {
      searchForm: {
        ...initData
      },
      pageInfo: {
        page: 1,
        limit: 5,
        total: 0
      },
      dataList: [],
      showImage: false,
      imageList: [],
      baseImage: process.env.VUE_APP_IMAGE_URL,
      props: {
        value: 'id',
        label: 'brandName',
        lazy: true,
        lazyLoad: (node, resolve) => {
          const parentId = node.data?.id ?? 0
          this.$http.get('brand/page', { parentId }).then(data => {
            if (data) {
              const rows = data.rows.map(item => {
                return {
                  ...item,
                  leaf: item.child === 0
                }
              })
              resolve(rows)
            }
          })
        }
      }
    }
  },
  methods: {
    getDataList () {
      this.searchForm.brandId = this.searchForm.ps[this.searchForm.ps.length - 1]
      this.$http.get('car/page', { ...this.searchForm, ...this.pageInfo }).then(data => {
        if (data) {
          this.dataList = [...data.rows]
          this.pageInfo.total = data.count
        }
      }).catch(() => {})
    },
    currentChange (page) {
      this.pageInfo.page = page
      this.getDataList()
    },
    sizeChange (limit) {
      this.pageInfo.limit = limit
      this.getDataList()
    },
    clearForm () {
      this.searchForm = { ...initData }
      this.pageInfo = {
        page: 1,
        limit: 5,
        total: 0
      }
      this.getDataList()
    },
    changeStatus (id, status) {
      this.$confirm(`确定更改编号<span style="font-weight: bold;color: #66b1ff">[${id}]</span>的状态为${status === 1 ? '<span style="color: green;font-weight: bold;">可用</span>' : '<span style="color: red;font-weight: bold;">禁用</span>'}吗?`, '询问', { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.post('car/status', { id, status }).then(() => {
          this.$message.success('更新状态成功')
          this.getDataList()
        }).catch(() => {})
      }).catch(() => {})
    },
    deleteInfo (id) {
      this.$confirm(`确定删除编号<span style="font-weight: bold;color: #66b1ff">[${id}]</span>的数据吗?`, '询问', { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.delete(`car/delete/${id}`).then(() => {
          this.$message.success('删除成功！')
          this.getDataList()
        }).catch(() => {})
      }).catch(() => {})
    },
    openDialog (id) {
      this.$refs.carUpdate.init(id)
    },
    showImageList (row) {
      this.$http.get('car/images/' + row.id).then(data => {
        if (data.length > 0) {
          this.imageList = data.map(item => {
            return { url: this.baseImage + item.carImage }
          })
          this.showImage = true
        } else {
          this.$message.warning('暂无图片')
        }
      })
    }
  },
  created () {
    this.getDataList()
  }
}
</script>

<style scoped>

</style>
<style>
.el-descriptions-item__cell{
  text-align: center !important;
}
</style>
